af<-log2(mergedata1)
numeric_columns<-sapply(mergedata1, is.numeric)# 使用 sapply 检查哪些列是数值型的，来源于百度但是找不到链接网址了
numeric_column_names<-names(mergedata1)[numeric_columns]# 获取数值型列的列名
af<-mergedata1
af[numeric_column_names]<-lapply(mergedata1[numeric_column_names], function(x) log2(x + (x == 0)))# 仅对数值列应用 log2 转换

pos=mergedata1==0;
mergedata1[pos]<-NA;
mergedata1[1,1]
af<-mergedata1
af[numeric_column_names] <- lapply(mergedata1[numeric_column_names], function(x) log2(x + (x == 0)))# 仅对数值列应用 log2 转换
af<-log2(mergedata1)
colnames(af)
group<-c("asym54","asym21","ad19","ctl63","asym26","ad01","ad50","ctl65","ad59","ad64","ctl17","asym60","ad16","asym35","ad37","ad11","ad56","ad41","ad38","ctl49","ctl58","ctl53","ad33","asym30","ad61","asym13","ad47","ad44","ctl18","ad40","ad51","ad15","x")
group<-gsub("\\d","",colnames(af))#计算每个种类各有多少
table(group)#可视化种类数目
View(group)
pro1<-af[i];#数据测试
pro1.1<-unlist(pro1);
pro1.2<-as.vector(t(pro1))
pro1.1

posctl<-group=="ctl"#提取各组数据
ctl<-pro1.1[posctl]
posad<-group=="ad"
ad<-pro1.1[posad]
posasym<-group=="asym"
asym<-pro1.1[posasym]

posx<-!is.na(ctl)#计算各组非0个数
ctlnum=sum(posx)
posx2<-!is.na(ad)
adnum=sum(posx2)
posx3<-!is.na(asym)
asymnum=sum(posx3)

if(asymnum>3&adnum>3&ctlnum>3)#如果数值都大于3就进行检验
{
  anovaresult<-oneway.test(pro1.1~group)
  p<-anovaresult$p.value
} else p = NA



library(ggplot2)
library(ggrepel)
install.packages("clusterProfiler") 
chooseCRANmirror()#更换镜像
#设置镜像 来于https://zhuanlan.zhihu.com/p/666913516
options("repos" = c(CRAN="http://mirrors.tuna.tsinghua.edu.cn/CRAN/"))#设置镜像 来于https://zhuanlan.zhihu.com/p/666913516
options(BioC_mirror="http://mirrors.tuna.tsinghua.edu.cn/bioconductor/")
#安装加载BiocManager包
if(!require("BiocManager")) install.packages("BiocManager",update = F,ask = F)
library(BiocManager)
BiocManager::install("clusterProfiler")
library(clusterProfiler)
BiocManager::install("org.Hs.eg.db")
library(org.Hs.eg.db)

load("E:/R Studio/shixi/shixi 4/volcano.RData")#加载volcano.RData
down<-prostat[prostat$P<0.05,]#值小于0.05的数据
allID<-prostat$ID[!is.na(prostat$P)]
enall<-enrichGO(down,'org.Hs.eg.db',keyType="UNIPROT",#富集分析
                ont='BP',pvalueCutoff=0.1,pAdjustMethod="none",
                minGSSize=10,maxGSSize=500,
                universe=allID)
plot_data<-enall[order(enall$geneRatio, decreasing = TRUE),]#绘制气泡图 https://zhuanlan.zhihu.com/p/580455609
ggplot(enall, aes(x=GeneRatio,y =Description)) +  
  geom_point(aes(size=Count),alpha=0.6,color="lightblue") +  
  theme(axis.text.x=element_text(angle=90,hjust=1)) +  
  xlab("GeneRatio") +  
  ylab("Description") +  
  ggtitle("GO Enrichment Analysis")